//
//  WLVistorView.swift
//  weibo
//
//  Created by 刁 海瑞 on 16/8/9.
//  Copyright © 2016年 毋磊. All rights reserved.
//

import UIKit
import SnapKit

class WLVistorView: UIView {
    
    var loginClosure: (()->())?
//    旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
//    罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//    主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    消息信息
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.textAlignment = .Center
        
        label.numberOfLines = 0
        
        return label
    }()
//    注册按钮
    private lazy var registerButton: UIButton = {
       
        let btn = UIButton()
        btn.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal )
        return btn
    }()
//    登录按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal )
        return btn
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    开启动画
    private func startAnimation() {
//        创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
//        设置核心动画的目的地
        animation.toValue = 2 * M_PI
//        设置动画时长
        animation.duration = 20
//        设置执行次数
        animation.repeatCount = MAXFLOAT
//        动画完成不让其释放
        animation.removedOnCompletion = false
//        执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    func updateVisitorViewInfo(message: String?, imageName: String?){
        
        if let meg = message, imgName = imageName{
            
            messageLabel.text = meg
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
        }else{
            
            cycleImageView.hidden = false
            startAnimation()
        }
        
    }
    
    
    
    private func setUpUI() {
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
    }
    
//    注册点击方法
    @objc private func registerButtonAction() {
        print("注册")
        
        loginClosure?()
    }
    
    @objc private func loginButtonAction() {
        print("登录")
        loginClosure?()
    }
    
}
